package Month07.day31;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class test5 {
    public static void main(String[] args) throws IOException {
        String[] strs = new String[]{"hello", "world", "java", "heiheihei"};
        HashMap<Character, Integer> map = new HashMap<>();
        for (String str : strs) {
            char[] chars = str.toCharArray();
            for (char aChar : chars) {
                if (!map.containsKey(aChar)) {
                    map.put(aChar, 1);
                } else {
                    map.put(aChar, map.get(aChar) + 1);
                }
            }
        }
        System.out.println(map);
        File file = new File("D:\\Idea-Project\\basic\\basic\\src\\Month07\\day31\\count3.txt");
        if (file.createNewFile()) {
            FileOutputStream fos = new FileOutputStream(file);
            Set<Map.Entry<Character, Integer>> entries = map.entrySet();
            Iterator<Map.Entry<Character, Integer>> iterator = entries.iterator();
            while (iterator.hasNext()) {
                Map.Entry<Character, Integer> next = iterator.next();
//            System.out.println(next.getKey()+":"+next.getValue());
                fos.write((next.getKey() + ":" + next.getValue() + "\r\n").getBytes());
            }
        }
    }
}
